$(function () {
    let id = sessionStorage.getItem("id-add-member");
    containUser(id);
    findAllNoGroupUser();

});

function containUser(id) {
    let result = myAjax('/member/findContainUser', 'post', {id: id});
    let html = "";
    let data = [];
    //每一个div里面添加了一个<input> 隐藏，目的是把成员用户id存进去，到后面剔除或添加成员的时候方便用到
    for (let i = 0; i < result.length; i++) {
        data.push(result[i].userId);
        data.push(result[i].userName);
        html += '<div><input type="checkbox" name="chooseUser" ' +
            'value="' + result[i].userName + '">' +
            '' + result[i].userName + '<input type="hidden" value="' + result[i].userId + '"></div>';
    }
    html += '   <div style="margin-left: 17%;position:fixed; bottom:0;">\n' +
        '            <input type="button"style="width: 80px;background-color:red;color:white;\n' +
        '            border-radius: 5px;"value="剔除" onclick="confirmCancel()">\n' +
        '             </div>';
    $("#added").html(html);
}

//从用户表查询所有未被分组的人员并动态显示在页面
function findAllNoGroupUser() {
    let result = myAjax('/user/findNoGroupUser', 'post', {});
    let html = "";
    for (let i = 0; i < result.length; i++) {
        html += '<div><input type="checkbox" name="selectUser"  value="' + result[i].name + '">' + result[i].name + '' +
            '<span hidden="hidden">' + result[i].id + '</span></div>';
    }
    $("#notAdded").html(html);
}

/**
 * 该方法做两件事：
 * 1、移动该组成员到右边未分组成员列表，
 * 2、剔除该组已有成员，剔除完并去后台把剔除成员所在小组信息删除，
 *    改变为未分组成员，更改user表小组状态，删除group_member表该人员信息,
 */
layui.use("layer");

function confirmCancel() {
    let data = {}
    //这里传一个userName其实就够了
    data.groupId = sessionStorage.getItem("id-add-member");
    data.userName = dataList("chooseUser");
    data.addTime = findDate();
    data.groupName = sessionStorage.getItem("add-new-group-member");
    if (data.userName != "") {
        let result = myAjax('/member/cancelMember', 'post', data);
        if (result === 1) {
            location.reload();
        } else {
            layer.msg("剔除失败！请重新选择");
        }
    }else {
        layer.msg("请先选择您要剔除的成员！")
    }
}

//该方法同样做两件事，与上面方法完全相反的两件事，上面是删除。这条是添加
function confirmMove() {
    let data = {}
    data.userName = dataList("selectUser");
    data.groupId = sessionStorage.getItem("id-add-member");
    data.addTime = findDate();
    data.groupName = sessionStorage.getItem("add-new-group-member");
    console.log(data.userName);
    if (data.userName != "") {
        let result = myAjax('/member/addNewMember', 'post', data);
        if (result === 1) {
            location.reload();
        } else {
            layer.msg("添加失败！请重新选择");
        }
    } else {
        layer.msg("请先选择您要添加的成员！")
    }
}

function dataList(data) {
    let vote = [];
    $('input:checkbox[name=' + data + ']').each(function () {
        if ($(this).prop('checked')) {
            vote.push($(this).val())
        }
    });
    return vote.join();
}

function findDate() {
    let dateTime = new Date();
    let year = dateTime.getFullYear();
    let month = dateTime.getMonth() + 1;
    let ri = dateTime.getDate();
    var hours = dateTime.getHours();
    var minutes = dateTime.getMinutes();
    var seconds = dateTime.getSeconds();
    return (year + "-" + month + "-" + ri + " " + hours + ":" + minutes + ":" + seconds);
}
